package com.ruoyi.develop.goodsReimbursement.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsFee.domain.GoodsFee;
import com.ruoyi.develop.goodsFee.service.IGoodsFeeService;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;
import com.ruoyi.export.service.impl.ExportServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsReimbursement.domain.GoodsReimbursement;
import com.ruoyi.develop.goodsReimbursement.service.IGoodsReimbursementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报销发票信息管理Controller
 * @author qh
 * @date 2023-01-12
 */
@RestController
@RequestMapping("/goodsReimbursement/goodsReimbursement")
public class GoodsReimbursementController extends BaseController {
    @Autowired
    private IGoodsReimbursementService goodsReimbursementService;
    @Autowired
    private ExportServiceImpl exportService;
    @Autowired
    private IGoodsOrderInService goodsOrderInService;
    @Autowired
    private IGoodsFeeService goodsFeeService;

    /**
     * 查询报销发票信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReimbursement:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsReimbursement entity) {
        startPage();
        List<GoodsReimbursement> list = goodsReimbursementService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出报销发票信息管理列表
     */
    /*@PreAuthorize("@ss.hasPermi('develop:GoodsReimbursement:export')")
    @Log(title = "报销发票信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsReimbursement entity) {
        List<GoodsReimbursement> list = goodsReimbursementService.selectList(entity);
        ExcelUtil<GoodsReimbursement> util = new ExcelUtil<GoodsReimbursement>(GoodsReimbursement. class);
        util.exportExcel(response, list, "报销发票信息管理数据");
    }*/

    /**
     * 获取报销发票信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReimbursement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsReimbursementService.selectById(id));
    }

    /**
     * 新增报销发票信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReimbursement:add')")
    @Log(title = "报销发票信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsReimbursement entity) {
        return toAjax(goodsReimbursementService.insert(entity));
    }

    /**
     * 修改报销发票信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReimbursement:edit')")
    @Log(title = "报销发票信息管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsReimbursement entity) {
        return toAjax(goodsReimbursementService.update(entity));
    }

    @PostMapping("/addNew")
    public AjaxResult addNew(@RequestBody GoodsReimbursement entity) {
        goodsReimbursementService.insertNew(entity);
        return AjaxResult.success(entity);
    }

    @PutMapping("/editNew")
    public AjaxResult editNew(@RequestBody GoodsReimbursement entity) {
        return toAjax(goodsReimbursementService.updateNew(entity));
    }

    /*@PutMapping("/edit0701")
    public AjaxResult edit0701(@RequestBody GoodsReimbursement entity) {
        return toAjax(goodsReimbursementService.edit0701(entity));
    }*/

    /**
     * 删除报销发票信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsReimbursement:remove')")
    @Log(title = "报销发票信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(goodsReimbursementService.deleteByIds(ids));
    }

    @Log(title = "报销明细信息导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GoodsOrderIn entity, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        String date = DateUtils.getTime();//默认当天
        startPage();
        List<GoodsOrderIn> list = goodsOrderInService.selectList(entity);
        list.stream().forEach(e->{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("buyDate", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, e.getBuyDate()));
            e.setParams(map);
        });
        params.put("list", list);
        params.put("date", date);
        String fileName = request.getParameter("fileName");
        if(StringUtils.isBlank(fileName)){
            fileName = "报销明细记录.xlsx";
        }
        String templateName = "报销明细记录.xlsx";
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
        return null;
    }

    @Log(title = "报销明细信息导出", businessType = BusinessType.EXPORT)
    @GetMapping("/exportFee")
    public AjaxResult exportFee(GoodsFee entity, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        String date = DateUtils.getTime();//默认当天
        startPage();
        List<GoodsFee> list = goodsFeeService.selectList(entity);
        list.stream().forEach(e->{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("type", DictUtils.getDictLabel("fee_type", e.getType()));
//            map.put("buyDate", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, e.getBuyDate()));
            e.setParams(map);
        });
        params.put("list", list);
        params.put("date", date);
        String fileName = request.getParameter("fileName");
        if(StringUtils.isBlank(fileName)){
            fileName = "费用报销明细记录.xlsx";
        }
        String templateName = "报销明细记录(费用).xlsx";
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
        return null;
    }
}
